import game from "./game";


const { ccclass, property } = cc._decorator;

@ccclass
export default class enemyCtrl extends cc.Component {
    isDie = false;
    action = cc.scaleTo(4, 0, 0);
    speed

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.init();
    }

    start() {

    }

    init() {
        this.isDie = false;
        let anim = this.getComponent(cc.Animation)
        anim.play("enemy1Normal");
        this.speed = 200 + Math.random() * 200;
    }

    hit() {
        let gg = cc.find("Canvas");
        let sc = gg.getComponent(game);
        sc.playSoundEnemyDie();
        this.isDie = true;
        let anim = this.getComponent(cc.Animation)
        anim.play("enemy1Boom");
        //console.log("hit" + this.isDie);


    }

    boomOver1() {
        this.isDie = true;
        let gg = cc.find("Canvas");
        let sc = gg.getComponent(game);
        sc.onEnemyKilled(this.node, 1);
        console.log("jia 1 ");
        sc.addScore(100/2);
    }



    // die(){
    //     this.isDie = true;
    //     // if(this.isDie==true){
    //     //    this.getComponent(cc.BoxCollider).enabled=false;
    //     //    console.log("okok111");
    //     // }
    //     //加载爆炸图片
    //     // cc.loader.loadRes("enemy0_die",cc.SpriteFrame,(err,res)=>{
    //     //    this.node.getComponent(cc.Sprite).spriteFrame = res;
    //     // });

    //     //加载爆炸动画
    //     let boom = this.getComponent(cc.Animation);
    //     boom.play("enemy1Boom");
    //     this.node.runAction(this.action);
    //     //300毫秒后销毁
    //     setTimeout(() => {
    //         this.node.destroy();
    //     }, 1000);
    // }

    update(dt) {
        let gg = cc.find("Canvas");
        let sc = gg.getComponent(game);
        if (this.isDie == false) {
            if (sc.gameType == 1 || sc.gameType == 3) {
                this.node.y -= this.speed * dt;
            }
        }


        if (this.node.y < -(cc.winSize.height / 2 + this.node.height / 2)) {
            sc.onEnemyKilled(this.node, 1);
            //console.log("enemy,kill");

        }

    }
}
